package com.tedu.weibo0521.base.exception;

import com.tedu.weibo0521.base.response.JsonResult;
import jakarta.validation.ConstraintViolationException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice
public class GlobalExceptionHandler {
    @ExceptionHandler
    public JsonResult doHandler(MethodArgumentNotValidException ex){
        String message = ex.getFieldError().getDefaultMessage();
        return JsonResult.failed(message);
    }
    @ExceptionHandler
    public JsonResult doHandlerConstraintViolationException(ConstraintViolationException ex) {
        String message = ex.getMessage();
        return JsonResult.failed(message);
    }

}
